/**
 * @ClassName: DicVipgradeDao
 * @author 830
 * @date 2016骞?10鏈?26鏃?
 */
package com.fzrskj.cloud.free.dic.dao;

import com.fzrskj.cloud.free.core.utils.CloudContextUtils;
import com.fzrskj.cloud.free.core.utils.CloudDbDrvUtils;
import com.fzrskj.cloud.free.dic.bean.DicVipgrade;
import ms.core.consts.SysErr;
import ms.core.db.IDbDrv;
import ms.core.object.CObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class DicVipgradeDao extends CObject{
	//private declarations
	private IDbDrv db=null;

	//constructor function to initialize database access object
	public DicVipgradeDao(){
		db= CloudDbDrvUtils.getDbUtilsDrv();
	}

	//to free resource
	@Override
	public void dispose(){
		db.closeConnection();
	}

	/**
	 * 根据code查询会员等级
	 *
	 * @param levelCode
	 * @return
	 */
	public DicVipgrade queryDicVipgradeByCode(String levelCode) {
		String sql = "select * from dic_vipgrade where levelCode = ? and agentId=?";
		DicVipgrade queryDicVipgradeByCode = db.queryForBean(sql, DicVipgrade.class, new Object[]{levelCode, CloudContextUtils.getAgentId()});
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return queryDicVipgradeByCode;
	}

	/**
	 * 根据code查询会员等级
	 *
	 * @param levelName
	 * @return
	 */
	public DicVipgrade queryDicVipgradeByName(String levelName) {
		String sql = "select * from dic_vipgrade where levelName = ? and agentId=?";
		DicVipgrade queryDicVipgradeByCode = db.queryForBean(sql, DicVipgrade.class, new Object[]{levelName, CloudContextUtils.getAgentId()});
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return queryDicVipgradeByCode;
	}

	public List<DicVipgrade> querySysVipgrade() {
		return this.querySysVipgrade(CloudContextUtils.getAgentId());
	}

	/**
	 * 查询全部的会员等级
	 *
	 * @return
	 */
	public List<DicVipgrade> querySysVipgrade(Integer agentId) {
		String sql = "select * from dic_vipgrade where agentId=? order by id asc";
		List<DicVipgrade> querySysVipgrade = db.queryForBeans(sql, DicVipgrade.class, new Object[]{agentId});
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return querySysVipgrade;
	}

	//to add new record to database by bean
	public int addDicVipgrade(DicVipgrade obj) {
		//if (!(obj.getAgentId() > 0)) {
			obj.setAgentId(CloudContextUtils.getAgentId());
		//}
		String sql = "insert into dic_vipgrade(levelCode,levelName,maxCharge,minCharge,rankIco," +
				"rebate,showIndex,agentId)" +
				"values(?,?,?,?,?," +
				"?,?,?);";
		Object[] prm = {obj.getLevelCode(), obj.getLevelName(), obj.getMaxCharge(), obj.getMinCharge(), obj.getRankIco(),
				obj.getRebate(), obj.getShowIndex(), obj.getAgentId()};
		int ret = db.executeSql(sql, prm);
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}

	public int addDicVipgrade(Map<String, Object> values) {
		values.put("agentId", CloudContextUtils.getAgentId());
		return this.addDicVipgradeFree(values);
	}

	//to add new record to database by map
	public int addDicVipgradeFree(Map<String, Object> values) {
		if (!values.containsKey("agentId")) {
			values.put("agentId", CloudContextUtils.getAgentId());
		}
		int ret = db.insertRecord("dic_vipgrade", values);
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}

	//to delete table record by id
	public int delDicVipgrade(int id) {
		String sql = "delete from dic_vipgrade where id=? and agentId=?";
		int ret = db.executeSql(sql, new Object[]{id, CloudContextUtils.getAgentId()});
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}

	//to delete table record by cdt
	public int delDicVipgrade(Map<String, Object> cdt) {
		//if (!cdt.containsKey("agentId")) {
			cdt.put("agentId", CloudContextUtils.getAgentId());
		//}
		int ret = db.deleteRecord("dic_vipgrade", cdt);
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}

	//to update table record by values and condition
	public int updateDicVipgrade(Map<String, Object> values, Map<String, Object> cdt) {
		//if (!cdt.containsKey("agentId")) {
			cdt.put("agentId", CloudContextUtils.getAgentId());
		//}
		int ret = db.modifyRecord("dic_vipgrade", values, cdt);
		String code = db.getErrCode();
		this.setErrCode(code);
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}

		return ret;
	}

	//to query dic_vipgrade item for detail
	public DicVipgrade queryDicVipgrade(int id) {
		String sql = "select * from dic_vipgrade where id=? and agentId=?";
		DicVipgrade ret = db.queryForBean(sql, DicVipgrade.class, new Object[]{id, CloudContextUtils.getAgentId()});
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}
	public int queryDicVipgrade(Map<String, Object> cdt) {
		cdt.put("agentId", CloudContextUtils.getAgentId());
		return queryDicVipgradeFree(cdt);
	}

	//to query DicVipgrade record count
	public int queryDicVipgradeFree(Map<String, Object> cdt) {
		if (!cdt.containsKey("agentId")) {
			cdt.put("agentId", CloudContextUtils.getAgentId());
		}
		List<Object> list = new ArrayList<>();
		StringBuilder sb = new StringBuilder("select count(*) from dic_vipgrade t where 1=1  ");
		for (Map.Entry<String, Object> c : cdt.entrySet()) {
			sb.append(" and " + c.getKey() + "=?");
			list.add(c.getValue());
		}

		Object[] params = new Object[list.size()];
		for (int i = 0; i < list.size(); i++) {
			params[i] = list.get(i);
		}
		Long ret = (Long) db.queryFieldValue(sb.toString(), Long.class, params, 1);
		this.setErrCode(db.getErrCode());
		return ret.intValue();
	}

	public List<DicVipgrade> queryDicVipgradeList(Map<String, Object> cdt, int page, int pagesize) {
		cdt.put("agentId", CloudContextUtils.getAgentId());
		return this.queryDicVipgradeListFree(cdt, page, pagesize);
	}

	//to query DicVipgrade for record list with condition,if page or pagesize less than 0, means without page
	public List<DicVipgrade> queryDicVipgradeListFree(Map<String, Object> cdt, int page, int pagesize) {
		if (!cdt.containsKey("agentId")) {
			cdt.put("agentId", CloudContextUtils.getAgentId());
		}
		List<Object> list = new ArrayList<>();
		StringBuilder sb = new StringBuilder("select  * from dic_vipgrade where 1=1 ");

		for (Map.Entry<String, Object> c : cdt.entrySet()) {
			sb.append(" and " + c.getKey() + "=?");
			list.add(c.getValue());
		}
		sb.append(" order by id ");
		String sql = sb.toString();
		if ((page > 0) && (pagesize > 0)) {
			sql = sql + String.format(" limit %d,%d", (page - 1) * pagesize, pagesize);
		}

		Object[] params = new Object[list.size()];
		for (int i = 0; i < list.size(); i++) {
			params[i] = list.get(i);
		}
		List<DicVipgrade> ret = db.queryForBeans(sql, DicVipgrade.class, params);
		this.setErrCode(db.getErrCode());
		return ret;
	}

	//根据会员Id查询会员等级
	public DicVipgrade queryDicVipgradeByUid(int userId) {
		return this.queryDicVipgradeByUidAppointAgent(userId, CloudContextUtils.getAgentId());
	}

	/**
	 * 根据会员Id和代理Id查询会员等级
	 * @param userId 用户ID.
	 * @param agentId 代理ID.
	 * @return 会员等级信息.
	 */
	public DicVipgrade queryDicVipgradeByUidAppointAgent(Integer userId, Integer agentId) {
		String sql = "select dv.* from user_account ua left join dic_vipgrade dv on ua.levelCode=dv.levelCode where " +
				" ua.userId=? and ua.agentId=? and dv.agentId=?";
		DicVipgrade ret = db.queryForBean(sql, DicVipgrade.class, new Object[]{userId, agentId, agentId});
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}
}